//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
// var time = baseJs.getCurrentTimeStr().datetime
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: baseJs.getCurrentTimeStr().datetime,
    imgUrl: baseJs.imgUrl,
    imgList: [],
    // 日期
    date: '2016-09-01',
    // 省市区
    region: ['', '', ''],
    // 性别
    gender: "女",
    genderId: 0,
    genderArr: [{
      value: "男",
      valueId: 1,
      active: false
    },
    {
      value: "女",
      valueId: 0,
      active: false
    }
    ],
    // 头像
    imgSrc: '',
    base64: "",
    // 遮罩层,性别
    genderFlag: true,
    uid: "",
    userType: "",
    fFlag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })
    this.GetUserInfoByID();
  },

  // 性别
  genderInputTap: function () {
    var that = this;
    var genderFlag = this.data.genderFlag;
    that.setData({
      genderFlag: false
    })
  },
  // 改变性别
  chooseGender: function (e) {
    var that = this;
    console.log(e)
    var genderArr = this.data.genderArr;
    var gender = this.data.gender;
    var genderId = this.data.genderId;
    genderId = e.currentTarget.dataset.valueid;
    var value = e.currentTarget.dataset.value;
    for (var i = 0, len = genderArr.length; i < len; ++i) {
      if (genderArr[i].value == value) {
        genderArr[i].active = true;
      } else {
        genderArr[i].active = false
      }
    }
    that.setData({
      genderArr: genderArr,
      value: value,
      gender: value,
      genderId: genderId
    })

  },
  confirm: function () {
    var that = this;
    var genderFlag = this.data.genderFlag;
    that.setData({
      genderFlag: true
    })

  },
  // 日期选择
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      fFlag: true,
    })
  },
  // 省市区
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  // 个人信息
  GetUserInfoByID: function () {
    var that = this;
    var uid = this.data.uid;
    var imgSrc = this.data.imgSrc;
    var gender = this.data.gender;
    var date = this.data.date;
    var region = this.data.region;
    var userData = this.data.userData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserInfoByID&UID=" + uid),
      success: function (res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        userData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        imgSrc = userData.HeaderImage;
        date = userData.Birthday;
        region[0] = userData.Province;
        region[1] = userData.City;
        region[2] = userData.District;
        if (userData.Sex == 0) {
          gender = "女"
        } else {
          gender = "男"
        }
        that.setData({
          userData: userData,
          imgSrc: imgSrc,
          gender: gender,
          date: date,
          region: region,
        })
        // console.log(userData);
      }
    })
  },
  // 保存
  formSubmit: function (e) {
    var that = this;
    var uid = this.data.uid;
    var genderId = this.data.genderId;
    var date = this.data.date;
    var region = this.data.region;
    var imgList = this.data.imgList;
    var imgStr = this.data.imgStr;
    imgStr = imgList.join(';');
    var imgUrl = this.data.imgUrl;
    console.log(e);
    var regPhone = /^1[3|4|5|7|8][0-9]{9}$/;
      if (!(regPhone.exec(e.detail.value.phone))) {
        wx.showToast({
          title: "您输入的手机号格式有误！",
          icon: 'none',
          duration: 2000
        });
        return;
      }
    
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=UpUserInfoByUID&UID=" + uid + "&Sex=" + genderId + "&Birthday=" + date + "&Email=" + e.detail.value.email + "&MobilePhone=" + e.detail.value.phone + "&QQ=" + e.detail.value.qq + "&detailAddress=" + e.detail.value.detailAddress + "&province=" + region[0] + "&city=" + region[1] + "&distinct=" + region[2] + "&UserTrueName=" + e.detail.value.realName + "&UserIdCard=" + e.detail.value.IDcard + "&CarNumber=" + e.detail.value.carNumber),
      success: function (res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        wx.showToast({
          title: JSON.parse(baseJs.decryUrl(res.data)).ResultMsg,
          icon: 'none',
          duration: 2000
        });
        wx.navigateBack()
      }
    })

  },
  // 修改头像
  chooseImg: function () {
    var that = this;
    var imgSrc = this.data.imgSrc;
    var imgUrl = this.data.imgUrl;
    wx.chooseImage({
      // 最多可以选择的图片张数，默认9
      count: 1,
      // 可以指定是原图还是压缩图，默认二者都有
      sizeType: ['original', 'compressed'],
      // 可以指定来源是相册还是相机，默认二者都有
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        // base64 = wx.arrayBufferToBase64(res.data);
        that.setData({
          imgSrc: tempFilePaths
        })
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0], //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            console.log(res);
            // console.log('data:image/png;base64,' + res.data)
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=LoadPic"),
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: 'post',
              data: baseJs.json2Form({
                UID:that.data.uid,
                HeadImage: res.data,
                type: 1,
              }),
              success: function (res) {
                console.log(res);
                imgSrc = imgUrl + res.data.ResultData;
                that.setData({
                  imgSrc: imgSrc,
                })
                console.log(imgSrc);
              }
            })
          },
          fail: err => {
            console.log(err);
          }
        })
      }
    })
  },
  // 
})